
import java.awt.Color;
import java.awt.Font;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.net.MalformedURLException;
import java.rmi.Naming;
import java.rmi.NotBoundException;
import java.rmi.RemoteException;
import java.sql.SQLException;

import javax.sql.rowset.WebRowSet;
import javax.swing.DefaultComboBoxModel;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.swing.JPasswordField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.table.DefaultTableModel;

public class Student_RegisterUI extends JFrame implements ActionListener{
	
	private JPasswordField txtPass;
	JButton btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate;
	
	JTable table;
	DefaultTableModel model;
	
	JComboBox cboUser,cboGrant;
	DefaultComboBoxModel userModel,grantModel;
	
	WebRowSet rs=null;
	DataProcess data=null;

	public Student_RegisterUI() {
		getContentPane().setLayout(null);
		
		try {
			try {
				data=(DataProcess)Naming.lookup("//khangutc/dkh");
			} catch (RemoteException e) {
				e.printStackTrace();
			}
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (NotBoundException e) {
			e.printStackTrace();
		}
		
		JLabel lblBangPhanQuyen = new JLabel("Bang Phan Quyen Truy Nhap");
		lblBangPhanQuyen.setFont(new Font("Tahoma", Font.PLAIN, 14));
		lblBangPhanQuyen.setForeground(Color.RED);
		lblBangPhanQuyen.setBounds(215, 11, 200, 26);
		getContentPane().add(lblBangPhanQuyen);
		
		JLabel lblUsername = new JLabel("UserName");
		lblUsername.setBounds(32, 61, 73, 26);
		getContentPane().add(lblUsername);
		
		JLabel lblPassword = new JLabel("PassWord");
		lblPassword.setBounds(309, 64, 66, 20);
		getContentPane().add(lblPassword);
		
		txtPass = new JPasswordField();
		txtPass.setBounds(385, 64, 194, 20);
		getContentPane().add(txtPass);
		
		/**
		 * ComboBox User
		 */
		userModel = new DefaultComboBoxModel();
		
		cboUser = new JComboBox(userModel);
		cboUser.setBounds(115, 64, 184, 20);
		getContentPane().add(cboUser);
		fillCombo();
		
		JLabel lblGrant = new JLabel("Grant");
		lblGrant.setBounds(32, 104, 46, 20);
		getContentPane().add(lblGrant);
		
		/**
		 * ComboBox Grant
		 */
		grantModel = new DefaultComboBoxModel();
		grantModel.addElement(0);
		grantModel.addElement(1);
		cboGrant = new JComboBox(grantModel);
		cboGrant.setBounds(115, 104, 184, 20);
		getContentPane().add(cboGrant);
		
		/**
		 * Table Settings		
		 */
		String[] header={"UserName","PassWord","Grant","LastLogin"};
		model= new DefaultTableModel(header,0);
		
		table= new JTable(model);
		table.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent arg0) {
				String user=String.valueOf(table.getValueAt(table.getSelectedRow(), 0));
				String pass=String.valueOf(table.getValueAt(table.getSelectedRow(), 1));
				String grant=String.valueOf(table.getValueAt(table.getSelectedRow(), 2));
				cboUser.setSelectedItem(user);
				txtPass.setText(pass);
				cboGrant.setSelectedItem(grant);
				btnDelete.setEnabled(true);
				btnUpdate.setEnabled(true);
				btnReset.setEnabled(true);
			}
		});
		fillTable();
		
		JScrollPane scrollPane = new JScrollPane(table);
		scrollPane.setBounds(10, 163, 583, 162);
		getContentPane().add(scrollPane);
		
		btnNew = new JButton("New");
		btnNew.setBounds(10, 336, 89, 23);
		getContentPane().add(btnNew);
		
		btnUpdate = new JButton("Update");
		btnUpdate.setBounds(211, 336, 89, 23);
		getContentPane().add(btnUpdate);
		
		btnDelete = new JButton("Delete");
		btnDelete.setBounds(310, 336, 89, 23);
		getContentPane().add(btnDelete);
		
		btnReset = new JButton("Reset");
		btnReset.setBounds(406, 336, 89, 23);
		getContentPane().add(btnReset);
		
		btnExit = new JButton("Exit");
		btnExit.setBounds(505, 336, 89, 23);
		getContentPane().add(btnExit);
		
		btnSave = new JButton("Save");
		btnSave.setBounds(109, 336, 89, 23);
		getContentPane().add(btnSave);
		
		JButton[] btn={btnNew,btnSave,btnReset,btnDelete,btnExit,btnUpdate};
		int i;
		for(i=0;i<btn.length;i++){
			btn[i].addActionListener(this);
		}

	}
	
	public static void main(String[] args){
		new Student_RegisterUI().setVisible(true);
	}
	
	/**
	 * Fill ComboBox Method
	 */
	protected void fillCombo(){
		String sql="Student";
		try {
			rs=data.GetTable(sql);
			while(rs.next()){
				userModel.addElement(rs.getString(1));
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Fill Table
	 */
	protected void fillTable() {
		
		String sql="Login";	
		try {
			rs=data.GetTable(sql);
			while(rs.next()){
				String[] row={rs.getString(1),rs.getString(2),String.valueOf(rs.getInt(3)),rs.getString(4)};
				model.addRow(row);
			}
		} catch (RemoteException e) {
			e.printStackTrace();
		} catch (SQLException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * Delete All Table Rows
	 */
	protected void deleteAllRows() {
		while(model.getRowCount()>0){
			model.removeRow(0);
		}
	}
	
	/**
	 * Update Table Rows
	 */
	protected void updateRow() {
		deleteAllRows();
		fillTable();		
	}
	
	/**
	 * Insert Data
	 */
	@SuppressWarnings("deprecation")
	protected boolean insertData() {
		
		String user=cboUser.getSelectedItem().toString();
		String pass=txtPass.getText();
		int grant = Integer.parseInt(cboGrant.getSelectedItem().toString());
		String lastlogin="";
		String sql="insert into Login values('" + user + "','" +pass+"'," + grant + ",'" + lastlogin + "')";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly delete 1 ban ghi
	 * @param id
	 * @return
	 */
	protected boolean deleteData(){
		String id=cboUser.getSelectedItem().toString();
		String sql="delete from Login where UserName='" + id + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Xu ly update du lieu
	 */
	protected boolean updateDate(String username, String pass, int grant){
		String sql="update Login set PassWord='" + pass + "',GrantUser=" + grant +
				" where UserName='" + username + "'";
		try {
			data.ExecUpdateSQL(sql);
			return true;
		} catch (RemoteException e) {
			e.printStackTrace();
		}
		return false;
	}
	
	/**
	 * Ham reset values
	 */
	protected void resetValues(){
		cboUser.setSelectedIndex(-1);
		txtPass.setText("");
		cboGrant.setSelectedIndex(-1);
		btnNew.setEnabled(true);
		btnDelete.setEnabled(false);
		btnSave.setEnabled(false);
		btnReset.setEnabled(false);
		btnUpdate.setEnabled(false);
	}

	@SuppressWarnings("deprecation")
	@Override
	public void actionPerformed(ActionEvent e) {

		if(e.getSource()==btnNew){
			resetValues();
			cboUser.requestFocus();
			btnSave.setEnabled(true);
			btnReset.setEnabled(true);
		}
		
		if(e.getSource()==btnSave){
			if(insertData()){
				JOptionPane.showMessageDialog(null,"Save successed!");
				updateRow();
			}else{
				JOptionPane.showMessageDialog(null,"Insert failed!");
			}
		}
		
		if(e.getSource()==btnReset){
			resetValues();
		}
		
		if(e.getSource()==btnDelete){
			if(deleteData()){
				JOptionPane.showMessageDialog(null,"Deleted!");
				updateRow();
			}
			else{
				JOptionPane.showMessageDialog(null,"Delete failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnUpdate){
			
			String user=cboUser.getSelectedItem().toString();
			String pass=txtPass.getText();
			int grant= Integer.parseInt(cboGrant.getSelectedItem().toString());	
			
			if(updateDate(user, pass, grant)){
				JOptionPane.showMessageDialog(null,"Updated!");
				updateRow();
			}
			else{
				JOptionPane.showMessageDialog(null,"Update failed!");
			}
			resetValues();
		}
		
		if(e.getSource()==btnExit){
			System.exit(0);
		}
	}
}
